package com.newegg.nlp.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.newegg.nlp.AppMain;
import com.newegg.nlp.arangodb.dao.AdjectiveDao;
import com.newegg.nlp.arangodb.dao.CategoryDao;
import com.newegg.nlp.arangodb.dao.CategoryItemDao;
import com.newegg.nlp.arangodb.dao.ItemAdjectiveDao;
import com.newegg.nlp.arangodb.dao.ItemDao;
import com.newegg.nlp.arangodb.dao.ItemKeywordDao;
import com.newegg.nlp.arangodb.dao.ItemNounDao;
import com.newegg.nlp.arangodb.dao.KeywordAdjectiveDao;
import com.newegg.nlp.arangodb.dao.KeywordDao;
import com.newegg.nlp.arangodb.dao.KeywordNounDao;
import com.newegg.nlp.arangodb.dao.NounDao;
import com.newegg.nlp.arangodb.dao.ReviewDao;
import com.newegg.nlp.arangodb.dao.ReviewItemDao;
import com.newegg.nlp.arangodb.model.Adjective;
import com.newegg.nlp.arangodb.model.Category;
import com.newegg.nlp.arangodb.model.CategoryItem;
import com.newegg.nlp.arangodb.model.Item;
import com.newegg.nlp.arangodb.model.ItemAdjective;
import com.newegg.nlp.arangodb.model.ItemKeyword;
import com.newegg.nlp.arangodb.model.ItemNoun;
import com.newegg.nlp.arangodb.model.KeywordAdjective;
import com.newegg.nlp.arangodb.model.KeywordNoun;
import com.newegg.nlp.arangodb.model.Noun;
import com.newegg.nlp.arangodb.model.Review;
import com.newegg.nlp.arangodb.model.ReviewItem;
import com.newegg.nlp.util.CoreNLPHelp;
import com.newegg.nlp.util.FileUtils;
import com.newegg.nlp.util.corenlp.Keyword;
import com.newegg.nlp.util.corenlp.Result;
import com.newegg.nlp.util.CoreNLPHelp.Language;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=AppMain.class)
public class ParseArangoDemo {
	
	@Autowired
	KeywordDao keywordDao;
	
	@Autowired
	NounDao nounDao;
	
	@Autowired
	AdjectiveDao adjectiveDao;
	
	@Autowired
	KeywordAdjectiveDao keywordAdjectiveDao;
	
	@Autowired
	KeywordNounDao keywordNounDao;
	
	@Autowired
	ItemDao itemDao;
	
	@Autowired
	ItemKeywordDao itemKeywordDao;
	
	@Autowired
	ItemNounDao itemNounDao;
	
	@Autowired
	ItemAdjectiveDao itemAdjectiveDao;
	
	@Autowired
	ReviewDao reviewDao;
	
	@Autowired
	ReviewItemDao reviewItemDao;
	
	@Autowired
	CategoryDao categoryDao;
	
	@Autowired
	CategoryItemDao categoryItemDao;
	
	@Test
	public void test1() throws Exception {
		CoreNLPHelp.useRemote("http://localhost", 9000);
		FileUtils.readerline("E:\\data\\aws_data_train\\aws_review\\zh\\dataset_zh.json", "GBK", line -> {
			try {
				JSONObject json = JSON.parseObject(line);
				System.out.println(json);
				//================  save category  =================
				String category = json.getString("product_category");
				Category obj_category = categoryDao.getOrCreate(category, category);
				
				//================  save item  =================
				String itemnumber = json.getString("product_id");
				Item obj_item = itemDao.getOrCreate(itemnumber, itemnumber);
				
				//================  category-item  =================
				categoryItemDao.saveNotExist(new CategoryItem(obj_category, obj_item));
				
				//================  save review  =================
				String reviewid = json.getString("review_id");
				Integer egg = json.getInteger("stars");
				Review obj_review = reviewDao.getOrCreate(reviewid, egg, json.getString("review_body"));
				
				//================  review item  =================
				reviewItemDao.saveNotExist(new ReviewItem(obj_item, obj_review));
				
				Result result = CoreNLPHelp.parse(Language.ZH, json.getString("review_body"));
				System.out.println(result);
				for (String noun : result.getNoun()) {
					Noun n = nounDao.getOrCreate(noun);
					itemNounDao.saveNotExist(new ItemNoun(obj_item, n));
				}
				for (String adjective : result.getAdjective()) {
					if(adjective.length() < 1) { continue; }
					Adjective a = adjectiveDao.getOrCreate(adjective);
					itemAdjectiveDao.saveNotExist(new ItemAdjective(obj_item, a));
				}
				for (Keyword keyword : result.keywords()) {
					com.newegg.nlp.arangodb.model.Keyword kw = keywordDao.getOrCreate(keyword.getKeyword());
					
					Noun noun = nounDao.getOrCreate(keyword.getNoun());
					keywordNounDao.saveNotExist(new KeywordNoun(noun, kw));
					
					if(keyword.getAdjective().length() > 1) {
						Adjective adjective = adjectiveDao.getOrCreate(keyword.getAdjective());
						keywordAdjectiveDao.saveNotExist(new KeywordAdjective(adjective, kw));
					}
					
					itemKeywordDao.saveNotExist(new ItemKeyword(obj_item, kw));
				}
			} catch (Exception e) { 
				e.printStackTrace();
			}
		});
	}
}
